/**
 * 
 */
package cn.nawang.ebeim.client.util;

import java.io.IOException;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;

import cn.nawang.ebeim.client.bean.IpMacBean;

/**
 * @ClassName: MACAddress
 * @Description: 获取电脑的mac地址
 * @author zhangjf
 * @date 2015年10月16日 下午2:36:12
 * @version v1.0
 */
public class MACAddress {
  
  public static void main(String[] args) throws IOException {
    System.out.println(getMACAddr());
  }
  
  public static String getComputerName(){
    Map<String,String> map = System.getenv();  
//    System.out.println("用户名 :"+map.get("USERNAME"));//获取用户名  
    return map.get("COMPUTERNAME");
//    System.out.println("计算机域名 :"+map.get("USERDOMAIN"));//获取计算机域名  
  }
  
  public static String getMACAddr() {
    List<IpMacBean> beans = new ArrayList<IpMacBean>();
    try {
      Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
      NetworkInterface networkInterface;
      Enumeration<InetAddress> inetAddresses;
      while (networkInterfaces.hasMoreElements()) {
        networkInterface = networkInterfaces.nextElement();
        inetAddresses = networkInterface.getInetAddresses();
        byte[] mac = networkInterface.getHardwareAddress();
        if (mac == null){
          continue;
        }
        IpMacBean ipMacBean = new IpMacBean();
        while (inetAddresses.hasMoreElements()) {
          InetAddress addr = inetAddresses.nextElement();
          if (addr != null) {
            String ip = addr.getHostAddress();
            if(addr instanceof Inet4Address){ // IPV4
              ipMacBean.setIpv4(ip);
            }else if(addr instanceof Inet6Address){ // IPV6
              ipMacBean.setIpv6(ip);
            }
          }
        }
        String macStr = "";
        for (byte b : mac) {
          macStr += toHexString(b) + "-";
        }
        if (macStr.length() > 0) {
          macStr = macStr.substring(0, macStr.length() - 1).toUpperCase();
        }
        ipMacBean.setMac(macStr);
        //设置mac是否是本机的
        ipMacBean.setIsSelf();
        beans.add(ipMacBean);
      }
      
      for (IpMacBean ipMacBean : beans) {
        if(ipMacBean.getIsSelf()){
          return ipMacBean.getMac();
        }
      }
    } catch (SocketException e) {
      e.printStackTrace();
    }
    return "";
  }
  
//  public static String getMACAddr() {
//    NetworkInterface netInterface = null;
//    String macStr = "";
//    try {
//      netInterface = NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
//      byte[] macAddr = netInterface.getHardwareAddress();
//      System.out.print("MAC ADDR:\t");
//      for (byte b : macAddr) {
//        macStr += toHexString(b) + "-";
//      }
//      if(macStr.length()>0){
//        macStr = macStr.substring(0, macStr.length()-1).toUpperCase();
//      }
//    } catch (Exception e) {
//      e.printStackTrace();
//      return "";
//    }
//    return macStr;
//  }

  public static String toHexString(int integer) {
    // 将得来的int类型数字转化为十六进制数
    String str = Integer.toHexString((int) (integer & 0xff));
    // 如果遇到单字符，前置0占位补满两格
    if (str.length() == 1) {
      str = "0" + str;
    }
    return str;
  }
}
